package com.shengyuan.manage_os.old.ying.erpm.controls;

import java.io.File;

//import net.sourceforge.opencamera.MainActivity;

import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PictureButton extends RelativeLayout implements OnClickListener {
	public static String PictureFileName ;
	public String FIELD, CarmeraID;
	public String mFileName, FileName, littleName, PictureLittle;
	private ImageView mImageView;
	private TextView title;
	private int ID_PICTURE = 10, ID_TEXTVIEW =11;
	private RelativeLayout.LayoutParams LayoutParams, textLayoutParams;
	public PictureButton(Context context) {
		super(context);
		init(context,null);
	}
	public PictureButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}
//		int resouceId = attrs.getAttributeResourceValue(null, "FileName", 0);
//        if (resouceId > 0) {
//            mFileName = context.getResources().getText(resouceId).toString();
//        } else {
//        	mFileName = "";
//        }
	//    private void init(Context context, AttributeSet attrs){   
//        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(   
//                                                    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);    
//        TextView txv = new TextView(context);   
//        txv.setId(1);   
//        txv.setText("txv1");   
//        TextView txv2 = new TextView(context);   
//        txv2.setId(2);   
//        txv2.setText("txv2");   
//        lp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);   
//        this.addView(txv, lp);   
//        lp.addRule(RelativeLayout.BELOW,1);   
//        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(   
//                LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);    
//        lp2.addRule(RelativeLayout.BELOW,1);   
//           
//        this.addView(txv2,lp2);   
//        lp.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);     //此时更改lp仍旧能够改变txv的位置。    
//           
//    } 
	private void init(Context context, AttributeSet attrs) {
		if (attrs!=null) {
			TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.OnceCamera);         
			if (a!=null) {
				FIELD = a.getString(R.styleable.OnceCamera_FileName);  
				CarmeraID = a.getString(R.styleable.OnceCamera_CarmeraID);
				a.recycle();
			}
		}
		
		FIELD = FIELD==null? "": FIELD;
		mFileName = FIELD + ".jpg";
		littleName = FIELD+"_1.jpg";
		FileName = MainApplication.cameraPath + mFileName;
		PictureLittle = MainApplication.cameraPath + littleName;
		
		mImageView = new ImageView(context);
		title = new TextView(context);
		if (!isInEditMode()) {
			ID_PICTURE = ID_PICTURE+ Utility.GetTimeID();
			ID_TEXTVIEW = ID_TEXTVIEW+ Utility.GetTimeID();
			mImageView.setId(ID_PICTURE);
			title.setId(ID_TEXTVIEW);
		}

		LayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
		textLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		
		title.setText(FIELD);
		title.setTextSize(18.0f);
		title.setBackgroundColor(Color.WHITE);
		title.setTextColor(Color.BLUE);
		title.setEllipsize(TruncateAt.MIDDLE);  
		title.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);  
		title.setSingleLine(true);  
		//textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
		//textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
		textLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
		
		addView(mImageView, LayoutParams);
		addView(title, textLayoutParams);
		mImageView.setOnClickListener(this);
		postInvalidate();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == ID_PICTURE) {
			//ECApplication.pictureButton = this;
			PictureFileName = FileName;
			File file = new File(PictureLittle);
			if (file.exists()) {
				file.delete();
			}
//			file = new File(FileName);
//			if (!file.exists()) {
//				try {
//					file.createNewFile();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//			Intent intent = new Intent( ECApplication.currentActivity, MainActivity.class);
//			if (CarmeraID!=null) {
//				intent.putExtra("Camerid", CarmeraID);
//				MainActivity.pButton = this;
//			}
//			ECApplication.currentActivity.startActivity(intent);
			
		MainApplication.currentActivity.StartCamera(PictureLittle);
//			Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//android.media.action.IMAGE_CAPTURE  android.provider.MediaStore.ACTION_IMAGE_CAPTURE
//			Uri imageFileUri = Uri.fromFile(new File( PictureButton.PictureFileName));
//			imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,imageFileUri);
//			ECApplication.Instance.startActivity(imageCaptureIntent);
		}
	}
	
	@Override
	public void postInvalidate() {
//		try {
			title.setTextColor(Color.GRAY);
			if (IsExist()) {
				title.setTextColor(Color.RED);
				File file = new File(PictureLittle);
				if (file.exists()) {
//					Drawable drawable = Drawable.createFromPath(PictureLittle);
					Bitmap bitmap = BitmapFactory.decodeFile(PictureLittle);
					if (bitmap!=null) {
//						mImageView.setBackgroundDrawable(drawable);
						mImageView.setImageBitmap(bitmap);
					}
				}
			}
//				Drawable drawable = Drawable.createFromPath( FileName );
//				if (drawable!=null) {
//					title.setTextColor(Color.RED);
//					File file = new File(PictureLittle);
//					if (!file.exists()) {
//						Bitmap bm = null;
//				        Options op = new Options();
//				        op.inSampleSize = 16;
//				        op.inJustDecodeBounds = false;
//				        bm = BitmapFactory.decodeFile(FileName, op);
//				        FileOutputStream out = new FileOutputStream(file);
//				        if(bm.compress(Bitmap.CompressFormat.JPEG, 20, out)){
//				        	out.flush();
//				        	out.close();
//				        }
//					}
//					drawable = drawable.createFromPath(PictureLittle);
//					//mImageView.setBackground(drawable);
//					if (drawable!=null) {
//						mImageView.setBackgroundDrawable(drawable);
//					}
//					
//				}
//			}
//		} catch (Exception e) {
//		}
		//super.postInvalidate();
	}
	public Boolean IsExist() {
		File file = new File(FileName);
		return file.exists();
	}
	
}
